/**
 * 文件名：QueneService.java
 * 版权： 航天恒星科技有限公司
 * 描述：〈描述〉
 * 修改时间：2019年4月20日
 * 修改内容：〈修改内容〉
 */
package com.zmkm.project.dbserver.entity;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

public class QueneService {
    public static void main(String[] args) {
        ArrayDeque<ResRequest> deque = new ArrayDeque<>();
        // PriorityQueue<ResRequest> deque = new PriorityQueue<>();
        deque.add(new ResRequest("FD-1", 1));
        deque.add(new ResRequest("FD-3", 3));
        deque.add(new ResRequest("FD-2", 2));
        deque.add(new ResRequest("FD-4", 4));
        deque.add(new ResRequest("FD-5", 5));
        deque.add(new ResRequest("FD-6", 6));
        deque.add(new ResRequest("FD-7", 7));
        Object[] dObjects = deque.toArray();
        for (ResRequest d : deque) {
            System.out.println("风洞：" + d.getWtCode() + "权重：" + d.weight + ",id:"
                + d.getRequestId());
        }
        System.out.println("--------------");
        Arrays.sort(dObjects);
        // 清空双端队列，在将心得顺序加入
        deque.clear();
        for (Object d : dObjects) {
            ResRequest dRequest = (ResRequest) d;
            deque.add(dRequest);
        }
        ResRequest ent = deque.peekFirst();
        System.out
            .println("---取出队首元素---" + ent.getWtCode() + "权重：" + ent.weight);
        dObjects = deque.toArray();
        Arrays.sort(dObjects);
        deque.clear();
        for (Object d : dObjects) {
            ResRequest dRequest = (ResRequest) d;
            deque.add(dRequest);
            System.out.println(
                "风洞：" + dRequest.getWtCode() + "权重：" + dRequest.weight);
        }
        ResRequest ent1 = deque.peekLast();
        System.out
            .println("---取出队尾元素---" + ent1.getWtCode() + "权重：" + ent1.weight);
        dObjects = deque.toArray();
        Arrays.sort(dObjects);
        for (Object d : dObjects) {
            ResRequest dRequest = (ResRequest) d;
            System.out.println(
                "风洞：" + dRequest.getWtCode() + "权重：" + dRequest.weight);
        }
        System.out.println("---修改风洞FD-5的权重改为2，先找到它---");
        int ii = 2;
        for (ResRequest en : deque) {
            if (en.getWtCode().equals("FD-5")) {
                en.weight = 2;
            }
            if (!en.getWtCode().equals("FD-5") && en.weight >= 2) {
                en.weight = ii + 1;
                ii++;
            }
        }
        dObjects = deque.toArray();
        Arrays.sort(dObjects);
        deque.clear();
        for (Object d : dObjects) {
            ResRequest dRequest = (ResRequest) d;
            deque.add(dRequest);
            System.out.println(
                "风洞：" + dRequest.getWtCode() + "权重：" + dRequest.weight);
        }
        Object[] objects = deque.toArray();
        System.out.println("排序之前--智能调度之前，原有的队列顺序");
        for (Object d : dObjects) {
            ResRequest dRequest = (ResRequest) d;
            deque.add(dRequest);
            System.out.println(
                "风洞：" + dRequest.getWtCode() + "权重：" + dRequest.weight);
        }
        
        System.out.println("排序之后--智能调度完成，不修改权限的情况");
        System.out.println("-------智能调度窗口的输入--------");
        Map<String, Integer> map = new HashMap<>();
        map.put("FD-1", 4);
        map.put("FD-2", 5);
        map.put("FD-3", 3);
        map.put("FD-4", 2);
        map.put("FD-5", 1);
        map.put("FD-6", 6);
        // 进行智能调度--冒泡排序
        Object temp = null;
        boolean resutl = false;
        for (int i = 0; i < objects.length - 1; i++) {
            for (int j = 0; j < objects.length - i - 1; j++) {
                ResRequest iRequest = (ResRequest) objects[j];
                ResRequest i1Request = (ResRequest) objects[j + 1];
                if (map.containsKey(iRequest.getWtCode())
                    && map.containsKey(i1Request.getWtCode())) {
                    resutl = true;
                }
                if (resutl && map.get(iRequest.getWtCode()) >= map
                    .get(i1Request.getWtCode())) {
                    temp = objects[j];
                    objects[j] = objects[j + 1];
                    objects[j + 1] = temp;
                }
                resutl = false;
            }
        }
        for (Object d : objects) {
            ResRequest dRequest = (ResRequest) d;
            deque.add(dRequest);
            System.out.println(
                "风洞：" + dRequest.getWtCode() + "权重：" + dRequest.weight);
        }
        System.out.println("人工干预并修改权限");
        for (int i = 0; i < objects.length; i++) {
            ResRequest enString = (ResRequest) objects[i];
            enString.weight = i + 1;
            objects[i] = enString;
        }
        
        // 将调整权限的队列重新放入队列
        deque.clear();
        for (Object d : objects) {
            ResRequest dRequest = (ResRequest) d;
            deque.add(dRequest);
            System.out.println(
                "风洞：" + dRequest.getWtCode() + "权重：" + dRequest.weight);
        }
        System.out.println("获取队列首元素:" + JSON.toJSONString(deque.getFirst()));
        System.out.println("获取队列尾元素:" + JSON.toJSONString(deque.getLast()));
        // 进行人工干预--将某个风洞的权限放在某个风洞的后面。
        // 如将FD-2 放在FD-5的后面，权限做相应的调整
        objects = deque.toArray();
        Arrays.sort(objects);
        String wtCode = "FD-7";
        String wtCode1BeiFang = "FD-5";
        boolean result = false;
        boolean result1 = false;
        int x = -1;
        int y = -1;
        int weight = -1;
        boolean sysy = false;
        for (int i = 0; i < objects.length; i++) {
            ResRequest request = (ResRequest) objects[i];
            if (wtCode == null || wtCode.equals("")) {
                if (!sysy) {
                    request.weight = request.weight + 1;
                }
                if (request.getWtCode().equals(wtCode1BeiFang)) {
                    request.weight = 1;
                    sysy = true;
                }
            }
            else {
                
                if (request.getWtCode().equals(wtCode)) {
                    System.out.println(
                        "找到存放对象：" + wtCode + "当前权重为：" + request.weight);
                    weight = request.weight;
                    x = i;
                    result = true;
                }
                if (request.getWtCode().equals(wtCode1BeiFang)) {
                    System.out.println("找到被存放对象：" + wtCode1BeiFang + "当前权重为："
                        + request.weight);
                    y = i;
                    result1 = true;
                }
                if (result && !result1 && x < i) {
                    request.weight = request.weight + 1;
                }
                if (result1 && result && x == i) {
                    System.out.println(
                        "存放对象和被存放对象都找到：" + request.getWtCode() + "当前权重由："
                            + request.weight + "变为:" + (request.weight - 1));
                    request.weight = request.weight - 1;
                    ResRequest requ = (ResRequest) objects[y];
                    requ.weight = request.weight + 1;
                    objects[y] = requ;
                }
                if (result1 && !result && y < i) {
                    System.out.println("找到>存放对象，切< 被存放对象之间的数据："
                        + request.getWtCode() + "当前权重由：" + request.weight
                        + "变为:" + (request.weight - 1));
                    request.weight = request.weight - 1;
                }
                if (result && result1 && y == i) {
                    request.weight = weight + 1;
                    result1 = false;
                    result = false;
                }
            }
        }
        System.out.println(wtCode1BeiFang + "放到" + wtCode + "的下面:");
        Arrays.sort(objects);
        deque.clear();
        for (Object d : objects) {
            ResRequest dRequest = (ResRequest) d;
            deque.add(dRequest);
            System.out.println(
                "风洞：" + dRequest.getWtCode() + "权重：" + dRequest.weight);
        }
        System.out.println("获取队列首元素:" + JSON.toJSONString(deque.getFirst()));
        System.out.println("获取队列尾元素:" + JSON.toJSONString(deque.getLast()));
        
        System.out.println("某些元素之后的元素进行排序，比如FD-4之后的进行排序:");
        //
        // 首先找出FD-4之后的元素
        System.out.println("首先找出FD-4之后的元素");
        objects = deque.toArray();
        Arrays.sort(objects);
        boolean re = false;
        int wei = -1;
        List<ResRequest> list = new ArrayList<>();
        List<ResRequest> list1 = new ArrayList<>();
        for (Object d : objects) {
            ResRequest dRequest = (ResRequest) d;
            list1.add(dRequest);
            if (dRequest.getWtCode().equals("FD-4")) {
                wei = dRequest.weight;
                re = true;
            }
            if (re && dRequest.weight > wei) {
                list.add(dRequest);
                list1.remove(dRequest);
            }
        }
        System.out.println("需要排序的队列：");
        Object[] oxx = list.toArray();
        // 进行智能调度--冒泡排序
        Map<String, Integer> map1 = new HashMap<>();
        map1.put("FD-1", 0);
        map1.put("FD-2", 5);
        map1.put("FD-3", 7);
        map1.put("FD-4", 8);
        map1.put("FD-5", 5);
        map1.put("FD-6", 6);
        Object temp1 = null;
        boolean resu = false;
        for (int i = 0; i < oxx.length - 1; i++) {
            for (int j = 0; j < oxx.length - i - 1; j++) {
                ResRequest iRequest = (ResRequest) oxx[j];
                ResRequest i1Request = (ResRequest) oxx[j + 1];
                if (map1.containsKey(iRequest.getWtCode())
                    && map1.containsKey(i1Request.getWtCode())) {
                    resu = true;
                }
                if (resu && map1.get(iRequest.getWtCode()) >= map1
                    .get(i1Request.getWtCode())) {
                    temp1 = oxx[j];
                    oxx[j] = oxx[j + 1];
                    oxx[j + 1] = temp1;
                }
                resu = false;
            }
        }
        for (int i = 0; i < oxx.length; i++) {
            ResRequest enString = (ResRequest) oxx[i];
            enString.weight = wei + i + 1;
            oxx[i] = enString;
        }
        for (int i = 0; i < oxx.length; i++) {
            list1.add((ResRequest) oxx[i]);
        }
        Object[] oString = list1.toArray();
        Arrays.sort(oString);
        deque.clear();
        System.out.println("只能修改某些风洞之后的排序");
        for (int i = 0; i < oString.length; i++) {
            ResRequest dRequest = (ResRequest) oString[i];
            deque.add(dRequest);
            System.out.println(
                "风洞：" + dRequest.getWtCode() + "权重：" + dRequest.weight);
        }
        int[] data = {11, 66, 33, 56, 77, 55};
        int minData = 0;
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data.length - i - 1; j++) {
                if (data[j] > data[j + 1]) {
                    minData = data[j + 1];
                    data[j + 1] = data[j];
                    data[j] = minData;
                }
            }
        }
        System.out.print(Arrays.toString(data));
    }
}
